{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37c13066",
   "metadata": {},
   "source": [
    "# Accessing Item Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fa9e41a",
   "metadata": {},
   "source": [
    "## Table of Contents\n",
    "- [Import Libraries](#Import-Libraries)\n",
    "- [Connect to ArcGIS Online](#Connect-to-ArcGIS-Online)\n",
    "- [Listing Item Resources](#Listing-Item-Resources)\n",
    "- [Analyzing Resource File Data](#Analyzing-Resource-File-Data)\n",
    "- [Exporting Resources](#Exporting-Resources)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e4e67ae",
   "metadata": {},
   "source": [
    "An `Item` object in the GIS will often have binary or textual data provided in the form of resource files, and to manage these files we've introduced a [`ResourceManager`](https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#resourcemanager) helper class. When an `Item` is initialized, this `ResourceManager` instance is created and stored as the `resources` property of that `Item`. This property then provides access to a number of methods useful for viewing and modifying the files, including: `list()`, `get()`, `add()`, `update()`, `remove()` and `export()`.\n",
    "\n",
    "Users will generally not interact with the `resources` property directly, nor will they create a `ResourceManager` instance directly. Instead they will more often create classes and call methods which in turn initialize a `ResourceManager` instance for a portal `Item` and leverage these methods under the hood.\n",
    "\n",
    "While the adding, updating, and removal of these resource files should ideally be accomplished through safer, higher-level functions and interfaces, users might be interested in directly calling the `list()`, `get()` and `export()` methods directly. Let's take a look at how we can use these methods and what we should expect as a response. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fcc607b",
   "metadata": {},
   "source": [
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8fd25219",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "\n",
    "import arcgis\n",
    "from arcgis.gis import GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "358020a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.9.1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arcgis.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5da6b28",
   "metadata": {},
   "source": [
    "## Connect to ArcGIS Online"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69e85c08",
   "metadata": {},
   "source": [
    "Connect to your ArcGIS Online portal with your profile to access the portal items available to you."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b9b594b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"9item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/user.html?user=api_data_owner' target='_blank'>\n",
       "                        <img src='https://geosaurus.maps.arcgis.com/home/js/arcgisonline/css/images/no-user-thumb.jpg' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\" style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/user.html?user=api_data_owner' target='_blank'><b>api_data owner</b>\n",
       "                        </a>\n",
       "                        <br/><br/><b>Bio</b>: None\n",
       "                        <br/><b>First Name</b>: api_data\n",
       "                        <br/><b>Last Name</b>: owner\n",
       "                        <br/><b>Username</b>: api_data_owner\n",
       "                        <br/><b>Joined</b>: April 10, 2019\n",
       "\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<User username:api_data_owner>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "profile_name = \"my_dev_profile\"\n",
    "\n",
    "gis = GIS(profile=profile_name)\n",
    "gis.users.me"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5be304fe",
   "metadata": {},
   "source": [
    "## Listing Item Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f78c3ba4",
   "metadata": {},
   "source": [
    "The resources belonging to a particular `Item` object can be returned using the `list()` method. This will return a list of dictionaries, where each dictionary represents a resource and contains information including the resource file name and size as well as the time it was created.\n",
    "\n",
    "Here we will query for a `StoryMap` object belonging to the current profile by using the `gis.content.get()` method and providing the id of that item."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bbfe8d9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=f1b6a842fbea45bca693c2fe6622bf70' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=f1b6a842fbea45bca693c2fe6622bf70' target='_blank'><b> Exposing patterns in land fires around the globe </b>\n",
       "                        </a>\n",
       "                        <br/>This story map analyzes global patterns in wildfires using spatial analysis tools in ArcGIS Pro with satellite thermal data. <img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/layers16.png' style=\"vertical-align:middle;\">StoryMap by api_data_owner\n",
       "                        <br/>Last Modified: January 12, 2021\n",
       "                        <br/>0 comments, 1 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\" Exposing patterns in land fires around the globe \" type:StoryMap owner:api_data_owner>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_id = 'f1b6a842fbea45bca693c2fe6622bf70'\n",
    "sm_item = gis.content.get(sm_id)\n",
    "sm_item"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54b57aec",
   "metadata": {},
   "source": [
    "Note that to get a full list of the portal items owned by the current user, we can use the `gis.content.search()` method as below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea3eb1e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1240 items returned belonging to current user\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<Item title:\"Automate Road Surface Investigation Using Deep Learning\" type:Notebook owner:api_data_owner>,\n",
       " <Item title:\"Pick_Pizza_Shops_San_Francisco\" type:Service Definition owner:api_data_owner>,\n",
       " <Item title:\"ofek_aerial_imagery_for_deadsea\" type:Service Definition owner:api_data_owner>,\n",
       " <Item title:\"england_weather_stations\" type:Shapefile owner:api_data_owner>,\n",
       " <Item title:\"Community_College_Dist\" type:Shapefile owner:api_data_owner>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_string = f\"owner: {gis.users.me.username}\"\n",
    "user_items = gis.content.search(query=query_string, max_items=-1)\n",
    "print(len(user_items), 'items returned belonging to current user')\n",
    "user_items[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "069f0aff",
   "metadata": {},
   "source": [
    "We can then call the [`list()`](https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ResourceManager.list) method to return a list of this Items resources."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3970511a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13 resources found for selected item\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'resource': '1584837341913.jpeg',\n",
       "  'created': 1610462633000,\n",
       "  'size': 3565854,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584840733269.png',\n",
       "  'created': 1610462633000,\n",
       "  'size': 36384,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584977960300.png',\n",
       "  'created': 1610462633000,\n",
       "  'size': 363718,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584995889818.png',\n",
       "  'created': 1610462634000,\n",
       "  'size': 13850,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584995908437.png',\n",
       "  'created': 1610462634000,\n",
       "  'size': 14455,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584996591808.png',\n",
       "  'created': 1610462634000,\n",
       "  'size': 11135,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1584996631292.png',\n",
       "  'created': 1610462634000,\n",
       "  'size': 11129,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1585069439012.png',\n",
       "  'created': 1610462635000,\n",
       "  'size': 6470,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1585080995091.png',\n",
       "  'created': 1610462635000,\n",
       "  'size': 152744,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1585081014377.png',\n",
       "  'created': 1610462635000,\n",
       "  'size': 143621,\n",
       "  'access': 'inherit'},\n",
       " {'resource': '1596760258881.jpeg',\n",
       "  'created': 1610462635000,\n",
       "  'size': 148737,\n",
       "  'access': 'inherit'},\n",
       " {'resource': 'oembed.json',\n",
       "  'created': 1610462636000,\n",
       "  'size': 676,\n",
       "  'access': 'inherit'},\n",
       " {'resource': 'oembed.xml',\n",
       "  'created': 1610462636000,\n",
       "  'size': 998,\n",
       "  'access': 'inherit'}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_resources = sm_item.resources.list()\n",
    "print(len(sm_resources), 'resources found for selected item')\n",
    "sm_resources"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac19b1c0",
   "metadata": {},
   "source": [
    "We can put this list into a Pandas DataFrame for easily comparing and analyzing the different objects returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "90b4c458",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>resource</th>\n",
       "      <th>created</th>\n",
       "      <th>size</th>\n",
       "      <th>access</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1584837341913.jpeg</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>3565854</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1584840733269.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>36384</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1584977960300.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>363718</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1584995889818.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>13850</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1584995908437.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>14455</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1584996591808.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>11135</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1584996631292.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>11129</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1585069439012.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>6470</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1585080995091.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>152744</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1585081014377.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>143621</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1596760258881.jpeg</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>148737</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>oembed.json</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>676</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>oembed.xml</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>998</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              resource        created     size   access\n",
       "0   1584837341913.jpeg  1610462633000  3565854  inherit\n",
       "1    1584840733269.png  1610462633000    36384  inherit\n",
       "2    1584977960300.png  1610462633000   363718  inherit\n",
       "3    1584995889818.png  1610462634000    13850  inherit\n",
       "4    1584995908437.png  1610462634000    14455  inherit\n",
       "5    1584996591808.png  1610462634000    11135  inherit\n",
       "6    1584996631292.png  1610462634000    11129  inherit\n",
       "7    1585069439012.png  1610462635000     6470  inherit\n",
       "8    1585080995091.png  1610462635000   152744  inherit\n",
       "9    1585081014377.png  1610462635000   143621  inherit\n",
       "10  1596760258881.jpeg  1610462635000   148737  inherit\n",
       "11         oembed.json  1610462636000      676  inherit\n",
       "12          oembed.xml  1610462636000      998  inherit"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(sm_resources)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c6835c8",
   "metadata": {},
   "source": [
    "We can see that this `StoryMap` item returned resources of a couple different file types, including several `jpeg` and `png` files as well as a `json` and `xml` file.\n",
    "\n",
    "Let's now look at a `Feature Layer Collection` object and see what types of resources it has available. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "78578907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=3d95a6aa9fa34243822138c7a9efc6b6' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=3d95a6aa9fa34243822138c7a9efc6b6' target='_blank'><b>Enriched_CrimeAnalysisData___Violent_Crime_2014</b>\n",
       "                        </a>\n",
       "                        <br/>Feature layer generated from Enrich layer<img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by api_data_owner\n",
       "                        <br/>Last Modified: June 19, 2021\n",
       "                        <br/>0 comments, 21 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Enriched_CrimeAnalysisData___Violent_Crime_2014\" type:Feature Layer Collection owner:api_data_owner>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs_id = '3d95a6aa9fa34243822138c7a9efc6b6'\n",
    "fs_item = gis.content.get(fs_id)\n",
    "fs_item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a2f7ec7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>resource</th>\n",
       "      <th>created</th>\n",
       "      <th>size</th>\n",
       "      <th>access</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>jobs/jf0e25a4606364e6992c440cf955b6e15.json</td>\n",
       "      <td>1624159076000</td>\n",
       "      <td>7741</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      resource        created  size   access\n",
       "0  jobs/jf0e25a4606364e6992c440cf955b6e15.json  1624159076000  7741  inherit"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(fs_item.resources.list())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad7c63ce",
   "metadata": {},
   "source": [
    "As we can see, this `Feature Layer Collection` has only one resource file available - a single `json` file.\n",
    "\n",
    "It is also possible for an `Item` to have no resource files, as we'll see when querying the `Web Map` object below. In this case, the `list()` method will return an empty list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e1fccf62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=a3e8eda445c34e95bdef7aa75bdd8a77' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://geosaurus.maps.arcgis.com/home/item.html?id=a3e8eda445c34e95bdef7aa75bdd8a77' target='_blank'><b>Coastline_India_l8</b>\n",
       "                        </a>\n",
       "                        <br/>Coastline_India<img src='https://geosaurus.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/maps16.png' style=\"vertical-align:middle;\">Web Map by api_data_owner\n",
       "                        <br/>Last Modified: February 10, 2021\n",
       "                        <br/>0 comments, 164 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Coastline_India_l8\" type:Web Map owner:api_data_owner>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wm_id = 'a3e8eda445c34e95bdef7aa75bdd8a77'\n",
    "wm_item = gis.content.get(wm_id)\n",
    "wm_item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "037be242",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wm_item.resources.list()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f631602e",
   "metadata": {},
   "source": [
    "## Analyzing Resource File Data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b3d350d",
   "metadata": {},
   "source": [
    "Let's look at what we can do with some of the raw resource outputs from the `list()` method. Here we create a DataFrame object with the resources returned from the `StoryMap` object earlier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b6fda0e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(13, 4)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>resource</th>\n",
       "      <th>created</th>\n",
       "      <th>size</th>\n",
       "      <th>access</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1584837341913.jpeg</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>3565854</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1584840733269.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>36384</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1584977960300.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>363718</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1584995889818.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>13850</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1584995908437.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>14455</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1584996591808.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>11135</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1584996631292.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>11129</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1585069439012.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>6470</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1585080995091.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>152744</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1585081014377.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>143621</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1596760258881.jpeg</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>148737</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>oembed.json</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>676</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>oembed.xml</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>998</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              resource        created     size   access\n",
       "0   1584837341913.jpeg  1610462633000  3565854  inherit\n",
       "1    1584840733269.png  1610462633000    36384  inherit\n",
       "2    1584977960300.png  1610462633000   363718  inherit\n",
       "3    1584995889818.png  1610462634000    13850  inherit\n",
       "4    1584995908437.png  1610462634000    14455  inherit\n",
       "5    1584996591808.png  1610462634000    11135  inherit\n",
       "6    1584996631292.png  1610462634000    11129  inherit\n",
       "7    1585069439012.png  1610462635000     6470  inherit\n",
       "8    1585080995091.png  1610462635000   152744  inherit\n",
       "9    1585081014377.png  1610462635000   143621  inherit\n",
       "10  1596760258881.jpeg  1610462635000   148737  inherit\n",
       "11         oembed.json  1610462636000      676  inherit\n",
       "12          oembed.xml  1610462636000      998  inherit"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_df = pd.DataFrame(sm_resources)\n",
    "print(sm_df.shape)\n",
    "sm_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64361f91",
   "metadata": {},
   "source": [
    "To return a single row in our DataFrame we can use the [`loc[]`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html) property by providing the corresponding index value in the leftmost column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e51df850",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "resource    1584837341913.jpeg\n",
       "created          1610462633000\n",
       "size                   3565854\n",
       "access                 inherit\n",
       "Name: 0, dtype: object"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_resource = sm_df.loc[0]\n",
    "sample_resource"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c20680b5",
   "metadata": {},
   "source": [
    "The `created` value returned with resource objects are in the form of a POSIX timestamp, which is an integer corresponding to the number of seconds since the current epoch began. For more information see [here](https://en.wikipedia.org/wiki/Unix_time). To convert this integer to a more familiar representation, we can use the `datetime` module along with its [`fromtimestamp()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp) method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bb7781a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 1, 12, 9, 43, 53)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = datetime.datetime.fromtimestamp(int(sample_resource.created/1000))\n",
    "dt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd6d69d",
   "metadata": {},
   "source": [
    "Our result is a `datetime` object, which we can display in string format with the `print()` command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9718e946",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-01-12 09:43:53\n"
     ]
    }
   ],
   "source": [
    "print(dt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0b85e7d",
   "metadata": {},
   "source": [
    "This string format is also how the datetime object will render in either a Pandas DataFrame or Series. Using the `apply()` method we can create a new Series of datetime objects which correspond to the values in the `created` column. For more information on the `apply()` method see [here](https://pandas.pydata.org/docs/reference/api/pandas.Series.apply.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2e8c55d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2021-01-12 09:43:53\n",
       "1    2021-01-12 09:43:53\n",
       "2    2021-01-12 09:43:53\n",
       "3    2021-01-12 09:43:54\n",
       "4    2021-01-12 09:43:54\n",
       "5    2021-01-12 09:43:54\n",
       "6    2021-01-12 09:43:54\n",
       "7    2021-01-12 09:43:55\n",
       "8    2021-01-12 09:43:55\n",
       "9    2021-01-12 09:43:55\n",
       "10   2021-01-12 09:43:55\n",
       "11   2021-01-12 09:43:56\n",
       "12   2021-01-12 09:43:56\n",
       "Name: created, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetimes = sm_df.created.apply(lambda x: datetime.datetime.fromtimestamp(int(x/1000)))\n",
    "datetimes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96fe0897",
   "metadata": {},
   "source": [
    "We can then insert this Series as a new column in our DataFrame using the [`insert()`]() method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dfe246c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>resource</th>\n",
       "      <th>created</th>\n",
       "      <th>created_datetime</th>\n",
       "      <th>size</th>\n",
       "      <th>access</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1584837341913.jpeg</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>2021-01-12 09:43:53</td>\n",
       "      <td>3565854</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1584840733269.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>2021-01-12 09:43:53</td>\n",
       "      <td>36384</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1584977960300.png</td>\n",
       "      <td>1610462633000</td>\n",
       "      <td>2021-01-12 09:43:53</td>\n",
       "      <td>363718</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1584995889818.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>2021-01-12 09:43:54</td>\n",
       "      <td>13850</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1584995908437.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>2021-01-12 09:43:54</td>\n",
       "      <td>14455</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1584996591808.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>2021-01-12 09:43:54</td>\n",
       "      <td>11135</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1584996631292.png</td>\n",
       "      <td>1610462634000</td>\n",
       "      <td>2021-01-12 09:43:54</td>\n",
       "      <td>11129</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1585069439012.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>2021-01-12 09:43:55</td>\n",
       "      <td>6470</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1585080995091.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>2021-01-12 09:43:55</td>\n",
       "      <td>152744</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1585081014377.png</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>2021-01-12 09:43:55</td>\n",
       "      <td>143621</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1596760258881.jpeg</td>\n",
       "      <td>1610462635000</td>\n",
       "      <td>2021-01-12 09:43:55</td>\n",
       "      <td>148737</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>oembed.json</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>2021-01-12 09:43:56</td>\n",
       "      <td>676</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>oembed.xml</td>\n",
       "      <td>1610462636000</td>\n",
       "      <td>2021-01-12 09:43:56</td>\n",
       "      <td>998</td>\n",
       "      <td>inherit</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              resource        created    created_datetime     size   access\n",
       "0   1584837341913.jpeg  1610462633000 2021-01-12 09:43:53  3565854  inherit\n",
       "1    1584840733269.png  1610462633000 2021-01-12 09:43:53    36384  inherit\n",
       "2    1584977960300.png  1610462633000 2021-01-12 09:43:53   363718  inherit\n",
       "3    1584995889818.png  1610462634000 2021-01-12 09:43:54    13850  inherit\n",
       "4    1584995908437.png  1610462634000 2021-01-12 09:43:54    14455  inherit\n",
       "5    1584996591808.png  1610462634000 2021-01-12 09:43:54    11135  inherit\n",
       "6    1584996631292.png  1610462634000 2021-01-12 09:43:54    11129  inherit\n",
       "7    1585069439012.png  1610462635000 2021-01-12 09:43:55     6470  inherit\n",
       "8    1585080995091.png  1610462635000 2021-01-12 09:43:55   152744  inherit\n",
       "9    1585081014377.png  1610462635000 2021-01-12 09:43:55   143621  inherit\n",
       "10  1596760258881.jpeg  1610462635000 2021-01-12 09:43:55   148737  inherit\n",
       "11         oembed.json  1610462636000 2021-01-12 09:43:56      676  inherit\n",
       "12          oembed.xml  1610462636000 2021-01-12 09:43:56      998  inherit"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_df.insert(2,'created_datetime',datetimes)\n",
    "sm_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a85aea6",
   "metadata": {},
   "source": [
    "We can also use the `value_counts()` method in pandas to get a breakdown of the number of occurences for each value in a particular `Series` (e.g. a column in a `DataFrame`). Below we parse the file extension from values in the `resource` column as a new `Series` object and use `value_counts()` to then get the number of each file type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "63baa71b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "png     9\n",
       "jpeg    2\n",
       "json    1\n",
       "xml     1\n",
       "Name: resource, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_df.resource.apply(lambda x: x.split('.')[-1]).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fd01a02",
   "metadata": {},
   "source": [
    "## Exporting Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0fc6b6e",
   "metadata": {},
   "source": [
    "Also included in the `ResourceManager` class is a method to export all resources as a zip file. This [`export()`](https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ResourceManager.export) method takes two parameters: `save_path` which declares the directory to save the zip file in and `file_name` which declares what name to give the zip file output. If no values are provided for either the `save_path` or `file_name` paramaters, then the zip file is uploaded to the default directory used for temporary files through [`tempfile.gettempdir()`](https://docs.python.org/3.7/library/tempfile.html#tempfile.gettempdir) and given a random 6 character name.\n",
    "\n",
    "Here we download the `StoryMap` resources as a zip file in our local directory using the `os.getcwd()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "311cd52c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip_name = \"storymap_resources.zip\"\n",
    "sm_item.resources.export(save_path=os.getcwd(), file_name=zip_name)\n",
    "\n",
    "# Check that the current directory now has that zip output\n",
    "os.path.isfile(zip_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9457bd81",
   "metadata": {},
   "source": [
    "If we'd like to download just a single resource file, then we can use the `get()` method and provide the file name. Note that if the resource file is of type `json` and there are no values provided for the `out_folder` and `out_file_name` parameters (which behave similarly to `save_path` and `file_name` above) then the result will be stored in local memory as a `dictionary` object rather than saved to a `json` file. This behaviour can be avoided, however, by setting `try_json=False`.\n",
    "\n",
    "For more information on using the `get()` method see [here](https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ResourceManager.get)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c1392479",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "oembed.json\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'version': '1.0',\n",
       " 'type': 'rich',\n",
       " 'title': 'Aufdecken von Mustern bei Flächenbränden rund um den Globus',\n",
       " 'url': 'https://storymaps.arcgis.com/stories/0f5ef7b723cd410f8e4e298d716bcd73',\n",
       " 'provider_name': 'ArcGIS StoryMaps',\n",
       " 'provider_url': 'https://storymaps.arcgis.com',\n",
       " 'width': 800,\n",
       " 'height': 600,\n",
       " 'thumbnail_url': 'https://www.arcgis.com/sharing/rest/content/items/0f5ef7b723cd410f8e4e298d716bcd73/info/thumbnail/ago_downloaded.jpg/',\n",
       " 'thumbnail_height': '266',\n",
       " 'thumbnail_width': '400',\n",
       " 'html': '<iframe src=\"https://storymaps.arcgis.com/stories/0f5ef7b723cd410f8e4e298d716bcd73\" width=\"800\" height=\"600\" scrolling=\"yes\" frameborder=\"0\" allowfullscreen></iframe>',\n",
       " 'cache_age': 86400}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the json file at position 11 in the resource DataFrame above\n",
    "file_name = sm_df.loc[11].resource\n",
    "print(file_name)\n",
    "\n",
    "# Output the dictionary returned from retrieving this json via get()\n",
    "sm_item.resources.get(file_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
